package com.zhz.thymeleafmp.web;

import com.zhz.thymeleafmp.entity.User;
import com.zhz.thymeleafmp.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class loginController {

    @Resource
    private UserService userService;

    @GetMapping({"/login","/"})
    public String login(){
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(String name, String password, HttpSession session) {
        User user = userService.login(name,password);
        if (user != null) {
            session.setAttribute("loginUser",user);
            return "redirect:/user/main";
        }
        return "login";
    }

    @GetMapping("/logOut")
    public String logOut(HttpSession session) {
        session.removeAttribute("loginUser");
        return "redirect:/login";
    }

}
